package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dlc.felear.xiaoensale.R;

import java.util.ArrayList;
import java.util.List;


public class LeftMenuCatAdapter extends BaseAdapter {

    private static final String TAG = "DialogCatAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<String> items;
    private int selectitem = -1;
    private List<Boolean> flags = new ArrayList<>();

    public LeftMenuCatAdapter(Context context) {
        this.context = context;
        items = new ArrayList<>();
    }

    public void setSelectitem(int selectitem) {
        this.selectitem = selectitem;
        notifyDataSetChanged();
    }

    public List<String> getData() {
        return items;
    }


    public void setItems(List<String> data) {
        if (data.isEmpty()) {
            notifyDataSetChanged();
            return;
        }
        items.clear();
        items.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public String getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.left_menu_cat_adapter, null);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (selectitem == position) {
            convertView.setBackgroundColor(Color.parseColor("#eecdcdcd"));
        } else {
            convertView.setBackgroundColor(Color.parseColor("#ffffffff"));
        }
        holder.tv_name.setText(items.get(position));
        return convertView;
    }

    static class ViewHolder {
        TextView tv_name;
    }

}